package com.simpou.commons.model.validation;

import com.simpou.commons.utils.validation.LocaleValidator;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import javax.validation.Payload;

/**
 * Java Beans validator: valida se string representa um locale.
 *
 * @author Jonas Pereira
 * @since 2012-10-01
 * @version 2012-10-01
 */
@Constraint(validatedBy = Locale.ValidLocaleImpl.class)
@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Locale {
    String MESSAGE = "{com.simpou.commons.model.validator.Locale.invalid}"
    ;
    String message() default MESSAGE;
    Class<?>[] groups() default  {
    }
    ;
    Class<?extends Payload>[] payload() default  {
    }
    ;
    public class ValidLocaleImpl implements ConstraintValidator<Locale,String> {
        @Override
        public void initialize(final Locale constraintAnnotation) {
        }

        @Override
        public boolean isValid(final String value,
            final ConstraintValidatorContext context) {
            return LocaleValidator.isValid(value) == null;
        }
    }
}
